package com.oauth.service.application.global;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import com.oauth.service.application.global.beans.Birthday;
import com.oauth.service.application.registry.GlobalRegistryService;
import com.oauth.service.application.registry.beans.ApplicationInfoBean;

/**
 * Use this service to do global functionality on the site.
 * 
 * @author asanthan
 * 
 */
public class GlobalServiceImpl implements GlobalService {

	/*
	 * Get birthdays from the next 20 days.
	 */
	private static final int BIRTHDAY_INTERVAL = 30;

	private GlobalRegistryService<ApplicationInfoBean> mGlobalRegistryService;

	public boolean updateStatus(Long pMemberId, Set<Long> pAppIds, String pStatusText) {
		List<ApplicationInfoBean> applicationList = mGlobalRegistryService
				.getApplicationInfo(pAppIds);
		for (ApplicationInfoBean appBean : applicationList) {
			appBean.getAppGlobalService().updateStatus(pMemberId, pStatusText);
		}
		return true;
	}

	@Autowired(required = true)
	public void setGlobalRegistryService(
			GlobalRegistryService<ApplicationInfoBean> pGlobalRegistryService) {
		mGlobalRegistryService = pGlobalRegistryService;
	}

	@Override
	public List<Birthday> getUpcomingBirthdays(Long pMemberId, Set<Long> pAppIds) {
		List<ApplicationInfoBean> applicationList = mGlobalRegistryService
				.getApplicationInfo(pAppIds);
		System.out.println("Hey Jrebel");
		List<Birthday> globalBirthdayList = new ArrayList<Birthday>();

		for (ApplicationInfoBean appBean : applicationList) {
			List<Birthday> birthdayList = appBean.getAppGlobalService().getUpcomingBirthdays(
					pMemberId, BIRTHDAY_INTERVAL);
			getBirthDayWithIconUrl(birthdayList, appBean);
			globalBirthdayList.addAll(birthdayList);
		}

		return globalBirthdayList;
	}

	private void getBirthDayWithIconUrl(List<Birthday> pBirthdayList, ApplicationInfoBean pAppBean) {

		for (Birthday birthday : pBirthdayList) {
			birthday.setApplicationIconUrl(pAppBean.getIconUrl());
		}
	}
}
